$(document).ready(function(){

	$('.form_datetime').datetimepicker({
		format: 'yyyy-mm-dd',
		language:  'zh-CN',
	    weekStart: 1,
	    todayBtn:  1,
	    autoclose: 1,
	    todayHighlight: 1,
	    startView: 2,
	    minView: 2,
	    forceParse: 1
	});	
	$("#querybtn2").bind("click",function(){
		var param = {};
		param.taskName = $("#taskName2").val().trim();
		param.createTimeBegin = $("#createTimeBegin2").val().trim();
		param.createTimeEnd = $("#createTimeEnd2").val().trim();
		main_page("queryTaskLogByPage.do",param,callback2,$("#pageFoot2"));
	});
	
	$("#saveTask").bind("click",function(){
		let param = {};
		param.taskName = $("#taskName").val();
		param.expectNum = $("#expectNum").val();
		param.taskDesc = $("#task_desc").val();
		$.ajax({
			type: "POST",
			url: "addTask.do",
			data: param,
			dataType:"json",//预期服务器返回的数据格式，然后做对象转换处理
			success: function(res){
				malert("code:"+res.resCode+"<br>desc:"+res.resDesc);
			},
			error: function(){
				malert("通讯错误，后台异常")
			}
		});
	});
	
	$("#submitQuery").bind("click",function(){
		var param = {};
		param.taskName = $("#taskNameQuery").val().trim();
		main_page("queryTaskByPage.do",param,callback,$("#pageFoot"));
	});
	
	$("#progressSubmit").bind("click",function(){
		var param = {};
		param.taskid=$("#pgTaskid").val();
		param.numStr = $("#addStarNum").val().trim();
		param.note = $("#addStarDesc").val().trim();
		$.ajax({
			type: "POST",
			url: "updateProcess.do",
			data: param,
			dataType:"json",
			success: function(res){
				malert("code:"+res.resCode+"<br>desc:"+res.resDesc);
				if("s0000" == res.resCode){
					$("#submitQuery").click();
					$("#task_progress_modal").modal("hide");
				}
			},
			error: function(){
				malert("通讯错误，后台异常")
			}
		});
	});
	
	$("#assignmentSubmit").bind("click",function(){
		var param = {};
		var url = "addAssignment.do";
		param.assiId = $("#assiId").text();
		if(param.assiId){
			url = "updateAssignment.do";
		}
		param.assiTaskId = $("#assiTaskId").val();
		param.assiExpectNum = $("#assiExpectNum").val();
		param.assiTaskModel = $("#assiTaskModel").val();
		param.assiCircleModel = $("#assiCircleModel").val();		
		param.assiState = $("#assiState").val();
		param.assiDesc = $("#assiDesc").val();
		$.ajax({
			type: "POST",
			url: url,
			data: param,
			dataType:"json",
			success: function(res){
				malert("code:"+res.resCode+"<br>desc:"+res.resDesc);
				if("s0000" == res.resCode){
					$("#assignment_modal").modal("hide");
					$("#submitQuery").click();
				}
			},
			error: function(){
				malert("通讯错误，后台异常")
			}
		});
	});
})


function callback(data){
    $("#data_body").empty();
    for (var i = 0; i < data.length; i++) {
        var obj = data[i];
        var $tr = $("<tr></tr>");
        var $td = $("<td></td>");
        $tr.append($td.clone().text(obj.taskName));
        $tr.append($td.clone().text(obj.createTime));
        $tr.append($td.clone().text(obj.updateTime));
        var progress = Math.floor(100*obj.starNum/obj.expectNum)+"%";//
        var progreessStr = "\
            <div class='progress'>\
                <div class='progress-bar' role='progressbar progress-bar-success' style='width: "+progress+";'>\
                </div>\
                "+progress+"\
            </div>";
        $tr.append($td.clone().html(progreessStr));
        $tr.append($td.clone().html(obj.starNum+"/"+obj.expectNum+" "+progress));
        var starBtn = $("\
                <button type='button' class='btn btn-success btn-xs' data-toggle='modal' data-target='#task_progress_modal'>\
                    <span class='glyphicon glyphicon-star-empty' aria-hidden='true'></span>\
                </button>\
            ");
        var detailBtn = $("<button type='button' class='btn btn-default btn-xs' data-toggle='modal' data-target='#task_detail_modal'>详情</button>");
        var assiBtn = $("<button type='button' class='btn btn-default btn-xs' data-toggle='modal' data-target='#assignment_modal'>分解目标</button>");
        if(obj.assiId){
            assiBtn.text("目标分解详情");
            if(obj.assiState == "01"){//已完成
                assiBtn.removeClass("btn-default");
                assiBtn.addClass("btn-success");
            }else if(obj.assiState == "02"){//暂停
                assiBtn.removeClass("btn-default");
                assiBtn.addClass("btn-warning");
            }
        }
        $tr.append($td.clone().append(starBtn).append(" ").append(detailBtn).append(" ").append(assiBtn));
        $tr.appendTo($("#data_body"));
        starBtn.bind("click",obj, function (e) {
            var obj = e.data;
            $("#addStarNum").val("");
            $("#addStarDesc").val("");
            $("#pgTaskid").val(obj.id);
            $("#task_progress_modal_title").text(obj.taskName+"  进度调整");
        });
        detailBtn.bind("click",obj, function (e) {
            var obj = e.data;
            $("#task_detail_modal td").text("");
            for (var i in obj) {
                $("#" + i + "Show").text(obj[i]);
            }
        });
        assiBtn.bind("click",obj, function (e) {
            var obj = e.data;
            $("#assignment_modal_title").text(obj.taskName+"_目标分解");
            $("#assiId").text(obj.assiId==undefined?"":obj.assiId);
            $("#assiTaskId").val(obj.id==undefined?"":obj.id);
            $("#addAssiExpectNum").val(obj.addAssiExpectNum==undefined?"":obj.addAssiExpectNum);
            $("#assiExpectNum").val(obj.assiExpectNum==undefined?"":obj.assiExpectNum);
            $("#assiTaskModel").val(obj.assiTaskModel==undefined?"":obj.assiTaskModel);
            $("#assiCircleModel").val(obj.assiCircleModel==undefined?"":obj.assiCircleModel);
            $("#assiState").val(obj.assiState==undefined?"":obj.assiState);
            $("#assiCreateTime").text(obj.assiCreateTime==undefined?"":obj.assiCreateTime);
            $("#assiUpdateTime").text(obj.assiUpdateTime==undefined?"":obj.assiUpdateTime);
            $("#assiRunTime").text(obj.assiRunTime==undefined?"":obj.assiRunTime);
            $("#assiDesc").val(obj.assiDesc==undefined?"":obj.assiDesc);
            $("#assignmentSubmit").text("新增");
    		if(obj.assiId){
    			$("#assignmentSubmit").text("更新");
    		}
        });
    }
}


function callback2(data){
    $("#data_body2").empty();
    for (var i = 0; i < data.length; i++) {
        var obj = data[i];
        var $tr = $("<tr></tr>");
        var $td = $("<td></td>");
        $tr.append($td.clone().text(obj.taskName));
        $tr.append($td.clone().text(obj.createTime));
        $tr.append($td.clone().text(obj.addStarNum));
        $tr.append($td.clone().text(obj.note?obj.note:""));
        var detailBtn = $("<button type='button' class='btn btn-default btn-xs' data-toggle='modal' data-target='#log_modal'>详情</button>");
        $tr.append($td.clone().append(detailBtn));
        $tr.appendTo($("#data_body2"));
        detailBtn.bind("click",obj, function (e) {
            var obj = e.data;
            $("#log_modal_title").html(obj.taskName+"<br>"+obj.createTime);
            $("#log_modal_content").html(obj.note?("addStarNum:"+obj.addStarNum+"<br>note:<br>"+obj.note):"");
        });
    }
}







